package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dto.AddRoleDTO;
import com.itheima.dto.RoleDTO;
import com.itheima.entity.SysMenu;
import com.itheima.entity.SysRole;
import com.itheima.mapper.SysMenuMapper;
import com.itheima.mapper.SysRoleMapper;
import com.itheima.service.SysRoleService;
import com.itheima.vo.MenuVO;
import com.itheima.vo.RoleInfoVO;
import com.itheima.vo.RolePageVO;
import com.itheima.vo.RoleVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl implements SysRoleService {

    private final SysRoleMapper sysRoleMapper;

    private final SysMenuMapper sysMenuMapper;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @Override
    public RolePageVO list(Integer page, Integer limit, String name) {
        RolePageVO rolePageVO = new RolePageVO();
        PageHelper.startPage(page,limit);
        List<SysRole> roleList = sysRoleMapper.selectByName(name);
        PageInfo<SysRole> pageInfo = PageInfo.of(roleList);
        rolePageVO.setCount(pageInfo.getTotal());
        rolePageVO.setTotalPages(pageInfo.getPages());
        rolePageVO.setPageSize(limit);
        rolePageVO.setCurrentPage(pageInfo.getPageNum());

        List<SysRole> list = pageInfo.getList();
        List<RoleVO> roleVOList = list.stream().map(new Function<SysRole, RoleVO>() {
            @Override
            public RoleVO apply(SysRole sysRole) {
                RoleVO roleVO = sysRole.transformRoleVO();
                roleVO.setRole_id(sysRole.getId());
                List<Integer> menuIds = sysRoleMapper.selectByRoleId(sysRole.getId());
                String strMenuIds = menuIds.stream().map(new Function<Integer, String>() {
                    @Override
                    public String apply(Integer menuId) {
                        return menuId.toString();
                    }
                }).collect(Collectors.joining(","));
                roleVO.setRole_menus(strMenuIds);
                return roleVO;
            }
        }).collect(Collectors.toList());

        rolePageVO.setData(roleVOList);

        return rolePageVO;
    }

    /**
     * 角色列表-全部
     * @return
     */
    @Override
    public List<RoleVO> allData() {
        List<SysRole> sysRoleList = sysRoleMapper.selectAllData();
        List<RoleVO> roleVOList = sysRoleList.stream().map(sysRole -> {
            RoleVO roleVO = sysRole.transformRoleVO();
            return roleVO;
        }).collect(Collectors.toList());
        return roleVOList;
    }

    /**
     * 角色添加
     * @param addRoleDTO
     */
    @Override
    public void save(AddRoleDTO addRoleDTO) {
        SysRole sysRole = new SysRole();
        //BeanUtils.copyProperties(addRoleDTO,sysRole);
        String roleId = UUID.randomUUID().toString();
        sysRole.setId(roleId);
        sysRole.setName(addRoleDTO.getName());
        sysRole.setDescription(addRoleDTO.getDescription());
        sysRole.setDelFlag('0');
        sysRole.setSuperadmin(0);
        sysRoleMapper.insert(sysRole);//添加到sys_role表

        //添加到sys_role_menu
        sysRoleMapper.insertRoleAndMenu(roleId,addRoleDTO.getMenuids());

    }

    /**
     * 角色查询回显
     * @param id
     * @return
     */
    @Override
    public RoleInfoVO rowInfo(String id) {
        SysRole sysRole = sysRoleMapper.selectById(id);
        RoleInfoVO roleInfoVO = sysRole.transformRoleInfoVO();

        List<SysMenu> sysMenuList = sysRoleMapper.selectListByRoleId(sysRole.getId());
        List<MenuVO> menuVOList = sysMenuList.stream().map(new Function<SysMenu, MenuVO>() {
            @Override
            public MenuVO apply(SysMenu sysMenu) {
                MenuVO menuVO = sysMenu.transformMenuVO();
                return menuVO;
            }
        }).collect(Collectors.toList());
        List<Integer> menuids = sysRoleMapper.selectMenuIdsByRoleId(sysRole.getId());
        Integer pid = menuids.get(0);
        menuVOList.forEach(menuVO -> {
            List<SysMenu> sysMenuList1 = sysMenuMapper.selectByPId(pid);
            List<MenuVO> menuVOList1 = sysMenuList1.stream().map(new Function<SysMenu, MenuVO>() {
                @Override
                public MenuVO apply(SysMenu sysMenu) {
                    MenuVO menuVO1 = sysMenu.transformMenuVO();
                    return menuVO1;
                }
            }).collect(Collectors.toList());
            menuVOList1.forEach(menuVO1 -> {
                List<SysMenu> sysMenuList2 = sysMenuMapper.selectByPId(menuVO1.getId());
                List<MenuVO> menuVOList2 = sysMenuList2.stream().map(new Function<SysMenu, MenuVO>() {
                    @Override
                    public MenuVO apply(SysMenu sysMenu) {
                        MenuVO menuVO2 = sysMenu.transformMenuVO();
                        return menuVO2;
                    }
                }).collect(Collectors.toList());
                menuVO1.setChildren(menuVOList2);
            });
            menuVO.setChildren(menuVOList1);
        });
        roleInfoVO.setAuthList(menuVOList);
        return roleInfoVO;
    }

    /**
     * 修改
     * @param roleDTO
     */
    @Override
    public void update(RoleDTO roleDTO) {
        sysRoleMapper.update(roleDTO);
        //根据role_id删除role_menu_id
        sysRoleMapper.delete(roleDTO.getId());
        List<Integer> menuids = roleDTO.getMenuids();
        sysRoleMapper.insertRoleAndMenu(roleDTO.getId(),menuids);
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void delete(String id) {
        sysRoleMapper.deleteRole(id);
    }
}
